package com.wjh.kt_wechat.service.impl;

import ch.qos.logback.core.read.ListAppender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wjh.kt_vod.exception.GgkException;
import com.wjh.kt_vod.vo.wechat.MenuVo;
import com.wjh.kt_wechat.entity.Menu;
import com.wjh.kt_wechat.mapper.MenuMapper;
import com.wjh.kt_wechat.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单明细 订单明细 服务实现类
 * </p>
 *
 * @author Wang_Junhong
 * @since 2022-07-17
 */
@Service
@Slf4j
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Override
    public List<Menu> findMenuOneInfo() {
        LambdaQueryWrapper<Menu> menuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        menuLambdaQueryWrapper.eq(Menu::getParentId, 0);
        return baseMapper.selectList(menuLambdaQueryWrapper);
    }

    @Override
    public List<MenuVo> findMenuInfo() {

//        创建集合封装最终数据
        ArrayList<MenuVo> menuVoArrayList = new ArrayList<>();
//        查询所有数据
        List<Menu> menuList = baseMapper.selectList(null);
//        封装一级数据
        List<Menu> oneMenuList = menuList.stream().filter(menu -> menu.getParentId().longValue() == 0).collect(Collectors.toList());
        oneMenuList.stream().forEach(one -> {
            MenuVo oneMenuVo = new MenuVo();
            BeanUtils.copyProperties(one, oneMenuVo);
            //        封装二级数据      判断id与parent_id是否相同
            List<Menu> twoMenuList = menuList.stream().filter(menu -> menu.getParentId().longValue() == one.getId()).collect(Collectors.toList());
            //二级数据集合
            ArrayList<MenuVo> childrenList = new ArrayList<>();

            twoMenuList.stream().forEach(two -> {
                MenuVo twoMenuVo = new MenuVo();
                BeanUtils.copyProperties(two, twoMenuVo);
                childrenList.add(twoMenuVo);
            });
            oneMenuVo.setChildren(childrenList);
            menuVoArrayList.add(oneMenuVo);
        });
        return menuVoArrayList;
    }

    @Autowired
    private WxMpService wxMpService;

    /**
     * 同步菜单方法
     */
    @Override
    public void syncMenu() {

        List<MenuVo> menuInfo = this.findMenuInfo();
        JSONArray buttonList = new JSONArray();
        menuInfo.stream().forEach(oneMeunVo -> {

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name",oneMeunVo.getName());

            //封装二级菜单
            JSONArray towMenuVo = new JSONArray();
            List<MenuVo> children = oneMeunVo.getChildren();

            children.stream().forEach(childrenMenuVo->{
                JSONObject view = new JSONObject();
                view.put("type",childrenMenuVo.getType());
                if (childrenMenuVo.getType().equals("view")){
                    view.put("name",childrenMenuVo.getName());
                    view.put("url","http://ggkt2.vipgz1.91tunnel.com/#"+childrenMenuVo.getUrl());
                }else {
                    view.put("name",childrenMenuVo.getName());
                    view.put("key",childrenMenuVo.getMeunKey());
                }
                towMenuVo.add(view);
            });
            jsonObject.put("sub_button",towMenuVo);
            buttonList.add(jsonObject);
        });

        JSONObject button = new JSONObject();
        button.put("button",buttonList);

        try {
            //同步菜单格式
            String s = wxMpService.getMenuService().menuCreate(button.toJSONString());
            log.info("meunId:",s);
        } catch (WxErrorException e) {
            throw new GgkException(20001,"公众号菜单同步失败");
        }
    }

    /**
     * 删除公众号菜单
     */
    @Override
    public void removeMenu() {
        try {
            wxMpService.getMenuService().menuDelete();
        } catch (WxErrorException e) {
            throw new GgkException(20001,"公众号菜单删除失败");
        }
    }

    /**
     * 公众菜单删除
     */


}
